<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head>
  <meta charset="utf-8">
  <title>我发起的</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" th:href="@{/layui/css/layui.css}" media="all">
  <link rel="stylesheet" th:href="@{/adminui/dist/css/admin.css}" media="all">
  <link rel="stylesheet" th:href="@{/style/custom.css}" media="all">
</head>
<body>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-form layui-card-header layuiadmin-card-header-auto layui-form-pane" lay-filter="LAY-instance-front-form">
      <div class="layui-form-item">
        <div class="layui-inline">
          <label class="layui-form-label">流程定义</label>
          <div class="layui-input-block">
            <select name="key" id="key" lay-search>
              <option value="">全部</option>
              <option th:each="map,idx:${procDefKeys}" th:value="${map.key}" th:text="${map.value}"></option>
            </select>
          </div>
        </div>
        <div class="layui-inline">
          <label class="layui-form-label">流程实例ID</label>
          <div class="layui-input-block">
            <input type="text" name="procInstId" placeholder="请输入" autocomplete="off" class="layui-input" lay-affix="clear">
          </div>
        </div>
        <div class="layui-inline">
          <label class="layui-form-label">业务流水号</label>
          <div class="layui-input-block">
            <input type="text" name="bizKey" placeholder="请输入" autocomplete="off" class="layui-input" lay-affix="clear">
          </div>
        </div>
        <div class="layui-inline">
          <button class="layui-btn layui-btn-normal layui-btn-sm" lay-submit lay-filter="LAY-instance-front-search">
            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i> 搜索
          </button>
          <button class="layui-btn layui-btn-primary layui-btn-sm" lay-submit lay-filter="LAY-instance-front-reset">
            <i class="layui-icon layui-icon-refresh layuiadmin-button-btn"></i> 重置
          </button>
        </div>
      </div>
    </div>

    <div class="layui-card-body">
      <div class="layui-btn-group" style="padding-bottom: 10px;">
        <!--刷新table-->
        <button type="button" class="layui-btn layui-btn-normal btn-op-instance layui-btn-sm" data-type="refresh">
          <i class="layui-icon">&#xe666;</i>刷新
        </button>
      </div>

      <!--table 定义-->
      <table id="LAY-instance-table" lay-filter="LAY-instance-table"></table>

      <!--列表操作项-->
      <script type="text/html" id="instance-table">
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="detail"><i class="layui-icon layui-icon-list"></i>详情</a>
        <a class="layui-btn layui-btn-xs" lay-event="more">更多 <i class="layui-icon layui-icon-down"></i></a>
      </script>
    </div>
  </div>
</div>

<script th:src="@{/layui/layui.js}"></script>
<script>
  layui.config({
    base: '/'
  }).extend({
    //设定模块别名，模块名是使用layui.模块名  模块文件是 js名字。不设置模块名 = 模块文件名
    instance: 'modules/flow/instance',
    fw: 'flow/fw'
  }).use(['instance', 'fw', 'flying'], function(){
    const $ = layui.$
        ,admin = layui.admin
        ,form = layui.form
        ,table = layui.table
        ,layer = layui.layer
        ,dropdown = layui.dropdown
        ,fw = layui.fw
        ,instance = layui.instance;

    //监听搜索
    form.on('submit(LAY-instance-front-search)', function (data) {
      let field = data.field;
      //执行重载
      table.reload('LAY-instance-table', {
        where: field
      });
    });
    // 监听重置
    form.on('submit(LAY-instance-front-reset)', function (data) {
      let field = data.field;
      for (let key of Object.keys(field)) {
        field[key] = '';
      }
      form.val("LAY-instance-front-form", field);
    });

    //事件
    let active = {
      refresh: function () {
        let data = form.val("LAY-instance-front-form");
        //执行重载 数据刷新
        table.reload('LAY-instance-table', {
          where: data
        });
      }
    };

    //执行事件操作
    layui.$('.layui-btn.btn-op-instance').on('click', function () {
      let type = layui.$(this).data('type');
      active[type] ? active[type].call(this) : '';
    });

    //监听工具条
    table.on('tool(LAY-instance-table)', function (obj) {
      let line_data = obj.data;

      if (obj.event === 'detail') {
        fw.handleDetail(line_data.procInstId, function () {
          //数据刷新
          table.reload('LAY-instance-table');
        });
      } else if (obj.event === 'more') {
        let more = new Array();
        if([[${#authorization.expression('hasAuthority("flow:inst:cancel")')}]]) {
          more.push({
            title: '撤回',
            id: 'cancel',
            templet: '<i class="layui-icon layui-icon-close"></i> 撤回',
            href: '#'
          })
        }
        if([[${#authorization.expression('hasAuthority("flow:inst:del")')}]]) {
          more.push({
            title: '删除',
            id: 'del',
            templet: '<i class="layui-icon layui-icon-delete"></i> 删除',
            href: '#'
          })
        }
        if([[${#authorization.expression('hasAuthority("flow:inst:start")')}]]) {
          more.push({
            title: '启动',
            id: 'start',
            templet: '<i class="layui-icon layui-icon-play"></i> 启动',
            href: '#'
          })
        }

        //下拉菜单
        dropdown.render({
          elem: this, // 触发事件的 DOM 对象
          show: true, // 外部事件触发即显示
          align: 'left ' , // 右对齐弹出
          data: more,
          click: function (mdata) {
            if (mdata.id === "del") {
              layer.confirm('真的删除行么？', function (index) {
                instance.handleDelete(index, line_data.procInstId);
              });
            } else if (mdata.id === "cancel") {
              // 撤回
              instance.handleCancel(line_data.procInstId);
            } else if (mdata.id === "start") {
              if ('cancel' !== line_data.businessStatus) {
                layer.msg('只能重新发起撤回的流程！', {icon: 2});
                return;
              }
              let obj = new Object();
              obj.procDefKey = line_data.procDefKey;
              obj.procInstId = line_data.procInstId;
              obj.bizKey = line_data.bizKey;
              obj.name = line_data.name;
              obj.variables = new Map();
              instance.handleStart(obj);
            }
          }
        });
      }
    });
  });
</script>
</body>
</html>
